
// 基础体温
var OVERVIEW_SHOW_DAYS_NUM = 15;
var firstOverviewShowValidTemperatureDate;
var lastValidTemperatureDate;
var bbtDateArray = new Array();
var bbtTemperatureArray = new Array();

// 月经
//用户所有的包括未来预测的月经开始，结束日期。
var customerDataYjqStartDateArray = new Array();
var customerDataYjqEndDateArray = new Array();

//今天之前的月经开始，结束日期数组。
var mensesStartDateArray = new Array();
var mensesEndDateArray = new Array();

var mensesDurationArray = new Array();
var mensesPeriodArray = new Array();
var mensesChartLabelArray = new Array();

var INTENT_SHOW_PERIOD_NUM = 6;

// 宫颈黏液
var MUCUS_SHOW_NUM = 16;
var mucusDateArray = new Array();
var mucusDataArray = new Array();
var mucusSeriesData = new Array();

function initOverviewChartData() {
	initYjqStartDateArray();
    initYjqEndDateArray();

    initBBTLineChartData();
    initMensesBarChartData();
    initMucusBarChartData();
}

function saveOverviewChartData(){
    saveCustomerDataYjqStartDateArrayStr2Local();
    saveCustomerDataYjqEndDateArrayStr2Local();

    // 由于Echarts画bbtChart和mucusChart占用大量内存可能导致OOM,
    // 故截取三个月经周期来画bbtChart和mucusChart;
    // 而月经分析图仍然用所有的数据。
    saveLimitedCustomerDateArrayStr2Local();

    saveValidOverviewBBTChartData2Local();
    saveMensesBarChartData2Local();
    saveMucusBarChartData2Local();
}

function initYjqStartDateArray() {
    mensesStartDateArray.length = 0;
    customerDataYjqStartDateArray.length = 0;

    for (var i = 0; i < customerDataJSONArrayLen; i++) {
        if (isPeriodStartDate(i)) {
            customerDate = customerDataJSONArray[i].date;

            if (isYjq(customerDate)){
            	addIntoYjqStartDateArray(customerDate);
            	
            	if(compareDateStr(customerDate, getCurrentDateStr()))
            		addIntoMensesStartDateArray(customerDate);
            }
        }
    }
}

function initYjqEndDateArray() {
    mensesEndDateArray.length = 0;
    customerDataYjqEndDateArray.length = 0;
	var mensesStartDateArrayLen = mensesStartDateArray.length;
	var j = 0;
	
    for (var i = 0; i < customerDataJSONArrayLen; i++) {
        if (isPeriodEndDate(i)) {
            customerDate = customerDataJSONArray[i].date;

            if (isYjq(customerDate)){
            	addIntoYjqEndDateArray(customerDate);
            	
            	if(j < mensesStartDateArrayLen){
            		addIntoMensesEndDateArray(customerDate);
            		j++;
            	}
            }
        }
    }
}

function isPeriodStartDate(i) {
    if (i == 0) return true;
    else {
        var previousCustomerPeriod = customerDataJSONArray[i - 1].period;
        var customerPeriod = customerDataJSONArray[i].period;

        if (previousCustomerPeriod != customerPeriod) return true;
        else return false;
    }
}

function isPeriodEndDate(i) {
    if (i == customerDataJSONArrayLen - 1) return true;
    else {
        var previousCustomerPeriod = customerDataJSONArray[i + 1].period;
        var customerPeriod = customerDataJSONArray[i].period;

        if (previousCustomerPeriod != customerPeriod) return true;
        else return false;
    }
}
function addIntoYjqStartDateArray(customerDate) {
    var yjqStartDateArrayLen = customerDataYjqStartDateArray.length;
    customerDataYjqStartDateArray[yjqStartDateArrayLen] = customerDate;
}

function addIntoMensesStartDateArray(customerDate){
	var mensesStartDateArrayLen = mensesStartDateArray.length;
    mensesStartDateArray[mensesStartDateArrayLen] = customerDate;
}

function addIntoYjqEndDateArray(customerDate) {
    var yjqEndDateArrayLen = customerDataYjqEndDateArray.length;
    customerDataYjqEndDateArray[yjqEndDateArrayLen] = customerDate;
}

function addIntoMensesEndDateArray(customerDate){
	var mensesEndDateArrayLen = mensesEndDateArray.length;
    mensesEndDateArray[mensesEndDateArrayLen] = customerDate;
}




/****************************************** 
  	 基础体温初始化
   *******************************************/

function initBBTLineChartData() {
    initLastValidTemperatureDate();
    initFirstOverviewShowValidTemperatureDate();

	initValidBBTNum();
  	//为了画出充满grid的曲线图，去掉两头可能存在的空白值
  	initValidOverviewBBTChartData();  	
}

  function initLastValidTemperatureDate(){
  	lastValidTemperatureDate = getLastValidTemperatureDate(customerDateArray.toString());
  }

  function initFirstValidTemperatureDate(){
    firstValidTemperatureDate = getFirstValidTemperatureDate(customerDateArray.toString());
  }

  function initFirstOverviewShowValidTemperatureDate(){
  	var tempFirstValidTemperatureDate = getFirstValidTemperatureDate(customerDateArray.toString());

  	 if(getDaysDuration(tempFirstValidTemperatureDate, lastValidTemperatureDate) > OVERVIEW_SHOW_DAYS_NUM)
  	    firstOverviewShowValidTemperatureDate = getCertainPreviousDateStr(lastValidTemperatureDate, OVERVIEW_SHOW_DAYS_NUM);
  	else
  	    firstOverviewShowValidTemperatureDate = tempFirstValidTemperatureDate;
  }

  function initValidBBTNum(){
  	//validBBTNum = getDaysDuration(firstValidTemperatureDate, lastValidTemperatureDate);  整个overview BBT画从第一条valid到最后一条valid BBT
  	validBBTNum = getDaysDuration(firstOverviewShowValidTemperatureDate, lastValidTemperatureDate);
  }
  
  function initValidOverviewBBTChartData(){
    var j = 0;

    for(var i = 0; i < customerDataJSONArrayLen; i++){
    	if(isValidDate(i)){
    		customerValidDateArray[j] = customerDateArray[i];
    		customerValidTemperatureArray[j] = customerDatePolishedTemperatureMap.get(customerDateArray[i]);
    		j++;
    	}
    }
  }
  
  function isValidDate(i){
  	return compareDateStr(customerDateArray[i],lastValidTemperatureDate) &&
                        compareDateStr(firstOverviewShowValidTemperatureDate, customerDateArray[i]);
  }
/****************************************** 
  	 月经初始化
   *******************************************/


function initMensesBarChartData() {
    atualShowPeriodNum = getActualMensesShowPeriod();

    mensesDurationArray = getMensesDurationArray();
    mensesPeriodArray = getMensesPeriodArray();
    mensesChartLabelArray = getMensesChartLabelArray();
}

//经期
function getMensesDurationArray() {
    //只需获取 Len - 1 个经期即可，因为周期个数为 Len - 1 个。
    for (var i = 0; i < atualShowPeriodNum; i++) {
        var tempStartDate = mensesStartDateArray[i];
        var tempEndDate = mensesEndDateArray[i];
        mensesDurationArray[i] = getDaysDuration(tempStartDate, tempEndDate);
    }

    return mensesDurationArray;
}


//周期
function getMensesPeriodArray() {
    //只需获取 Len - 1 个经期即可，因为周期个数为 Len - 1 个。
    for (var i = 0; i < atualShowPeriodNum; i++) {
        var oldStartDate = mensesStartDateArray[i];
        var newStartDate = mensesStartDateArray[i + 1];
        mensesPeriodArray[i] = getDaysDuration(oldStartDate, newStartDate) - 1;
    }

    return mensesPeriodArray;
}


function getMensesChartLabelArray(){
	for(var i = 0; i < INTENT_SHOW_PERIOD_NUM; i++){
	    if(i < atualShowPeriodNum)
	        mensesChartLabelArray[i] = mensesDurationArray[i] + "/" + mensesPeriodArray[i];
	    else
	        mensesChartLabelArray[i] = getLabel(i);
	}

    return mensesChartLabelArray;
}

function getLabel(num){
    var labelStr = "";
    for(var i = 0; i< num; i++){
        labelStr += " ";
    }

    return labelStr;
}

function getActualMensesShowPeriod() {
    return getMin(INTENT_SHOW_PERIOD_NUM, mensesStartDateArray.length - 1);
}

function getMin(m, n) {
    if (m <= n) return m;
    else return n;
}

/****************************************** 
  	 宫颈粘液初始化
   *******************************************/

function initMucusBarChartData() {
	mucusDateArray = getRoundDate(getCurrentDateStr(), MUCUS_SHOW_NUM, 0);

	for(var i = 0; i <= MUCUS_SHOW_NUM; i++){
		mucusDataArray[i] = customerDateMucusMap.get(mucusDateArray[i]);
	}
}

/******************************************
  	 存储Overview画图所需本地变量
   *******************************************/

function saveCustomerDataYjqStartDateArrayStr2Local(){
    var yjqStr = customerDataYjqStartDateArray.toString();
    var mensesStr = mensesStartDateArray.toString();
    if ((yjqStr == null) || (yjqStr == "")) {
        var customerDateStr = localStorage.getItem("customerDateKey");
        var customerDateArray = getCustomerDateArray(customerDateStr);
        yjqStr = customerDateArray[0];
    }
    localStorage.setItem("customerDataYjqStartDateArrayStrKey", yjqStr);
    localStorage.setItem("customerDataMensesStartDateArrayStrKey", mensesStr);
}

function saveCustomerDataYjqEndDateArrayStr2Local(){
    var yjqStr = customerDataYjqEndDateArray.toString();
    var mensesStr = mensesEndDateArray.toString();
    if ((yjqStr == null) || (yjqStr == "")) {
        var customerDateStr = localStorage.getItem("customerDateKey");
        var customerDateArray = getCustomerDateArray(customerDateStr);
        yjqStr = customerDateArray[customerDateArray.length - 1];
    }
    localStorage.setItem("customerDataYjqEndDateArrayStrKey", yjqStr);
    localStorage.setItem("customerDataMensesEndDateArrayStrKey", mensesStr);
}


/*
    S1 ---> S2 Current --> S3 --> S4
    S2 --> S3 为当前周期
    S3 --> S4 为下一个周期
    S1 --> S2 为上一个周期

    正常情况下(即S1-S4 <= 90天):
    S1 是截取的第一天
    S4 的前一天是截取的最后一天

    其他情况截取今天之前的90天
*/

 var limitedCustomerStartDateStr; // S1
 var limitedCustomerEndDateStr; // S4 的前一天
 var LIMITED_CUSTOMER_DATE_DURATION = 90; // 目前最多显示 90 天，确保不OOM

function saveLimitedCustomerDateArrayStr2Local(){
    var previousPeriodYjqStartDate; // S1
    var currentPeriodYjqStartDate; // S2
    var nextPeriodYjqStartDate; // S3
    var finalPeriodYjqStartDate; //S4

    if (customerDataYjqStartDateArray.length === 1) {
        previousPeriodYjqStartDate = customerDataJSONArray[0].date;
        currentPeriodYjqStartDate = customerDataYjqStartDateArray[0];
        finalPeriodYjqStartDate = customerDataJSONArray[customerDataJSONArray.length - 1].date;
        nextPeriodYjqStartDate = finalPeriodYjqStartDate;
    } else if (customerDataYjqStartDateArray.length === 0) {
        previousPeriodYjqStartDate = customerDataJSONArray[0].date;
        currentPeriodYjqStartDate = previousPeriodYjqStartDate;
        finalPeriodYjqStartDate = customerDataJSONArray[customerDataJSONArray.length - 1].date;
        nextPeriodYjqStartDate = finalPeriodYjqStartDate;
    } else {
        for(var i = customerDataYjqStartDateArray.length - 1; i >= 0; i--){
            if(compareDateStr(customerDataYjqStartDateArray[i], getCurrentDateStr())){
                currentPeriodYjqStartDate = customerDataYjqStartDateArray[i];

                if(i - 1 >= 0){
                    previousPeriodYjqStartDate = customerDataYjqStartDateArray[i - 1];
                } else {
                    //说明上一个月经期开始没有出现，此时把第一条数据赋给S1.
                    previousPeriodYjqStartDate = customerDataJSONArray[0].date;
                }
                nextPeriodYjqStartDate = customerDataYjqStartDateArray[i + 1];

                if(customerDataYjqStartDateArray.length > i + 2)
                    finalPeriodYjqStartDate = customerDataYjqStartDateArray[i + 2];
                else
                    //说明预测的数据里没有到达S4，此时把最后一条赋给S4.
                    finalPeriodYjqStartDate = customerDataJSONArray[customerDataJSONArray.length - 1].date;

                break;
            }
        }
    }

    if(getDaysDuration(previousPeriodYjqStartDate, getYesterdayDateStr(finalPeriodYjqStartDate)) <= LIMITED_CUSTOMER_DATE_DURATION ){
        limitedCustomerStartDateStr = previousPeriodYjqStartDate;
        limitedCustomerEndDateStr = getYesterdayDateStr(finalPeriodYjqStartDate);
    }else{
        var last90DaysArray = getRoundDate(getCurrentDateStr(), LIMITED_CUSTOMER_DATE_DURATION, 0);
        limitedCustomerStartDateStr = last90DaysArray[0];
        limitedCustomerEndDateStr = last90DaysArray.pop();
    }

    saveLimitedCustomerDate2Local(limitedCustomerStartDateStr, limitedCustomerEndDateStr);
}


function saveLimitedCustomerDate2Local(limitedCustomerStartDateStr, limitedCustomerEndDateStr){
    var limitedCustomerDateArray = new Array();
    var customerDateStr = localStorage.getItem("customerDateKey");
    var customerDateArray = getCustomerDateArray(customerDateStr);

    var j = 0;
    for(var i = 0; i < customerDateArray.length; i++){
        if(isInLimitedCustomerDate(limitedCustomerStartDateStr, i, limitedCustomerEndDateStr)){
            limitedCustomerDateArray[j] = customerDateArray[i];
            j++;
        }
    }

    saveCustomerData2Local("limitedCustomerDateArrayKey", limitedCustomerDateArray.toString());
    saveCustomerData2Local("limitedCustomerDateArrayLenKey", limitedCustomerDateArray.length);
}

function isInLimitedCustomerDate(limitedCustomerStartDateStr, i, limitedCustomerEndDateStr){
    return compareDateStr(limitedCustomerStartDateStr, customerDateArray[i]) && compareDateStr(customerDateArray[i], limitedCustomerEndDateStr);
}

function saveValidOverviewBBTChartData2Local(){
    saveCustomerData2Local("validBBTNumKey", validBBTNum);
    saveCustomerData2Local("customerValidDateArrayKey", customerValidDateArray.toString());
    saveCustomerData2Local("customerValidTemperatureArrayKey", customerValidTemperatureArray.toString());
}

function saveMensesBarChartData2Local(){
    saveCustomerData2Local("mensesChartLabelArrayKey", mensesChartLabelArray.toString());
    saveCustomerData2Local("mensesPeriodArrayKey", mensesPeriodArray.toString());
    saveCustomerData2Local("mensesDurationArrayKey", mensesDurationArray.toString());
}

function saveMucusBarChartData2Local(){
    saveCustomerData2Local("atualMensesShowPeriodNumKey", atualShowPeriodNum);
    saveCustomerData2Local("mucusDateArrayKey", mucusDateArray.toString());
    saveCustomerData2Local("mucusDataArrayKey", mucusDataArray.toString());
}